//
//  ThumbnailViewController.m
//  Test
//
//  Created by 杨德龙 on 2017/9/24.
//  Copyright © 2017年 ___杨德龙___. All rights reserved.
//

#import "ThumbnailViewController.h"
#import "ScribbleThumbnailCell.h"    // thumbnailCell
#import "CoordinatingController.h"    //
#import "ScribbleManager.h"    // scribbleManager
#import "CommandBarButton.h"   

@interface ThumbnailViewController ()

@end

@implementation ThumbnailViewController
{
    ScribbleManager *scribbleManager_;
    
    __weak IBOutlet UINavigationItem *navItem;
}

- (void)viewDidLoad
{
    //
    [super viewDidLoad];
    [self initUI];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----  初始化
// 初始化
- (void)initUI
{
    // 这样可以 不用加imageView 设置 view 和 image 一样的颜色
    UIColor *backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_texture"]];
    [self.view setBackgroundColor:backgroundColor];
    
    //
    scribbleManager_ = [[ScribbleManager alloc] init];
    NSInteger numberOfScribbles = [scribbleManager_ numberOfScribbles];
    // navItem 设置 title
    [navItem setTitle:[NSString stringWithFormat:
                       numberOfScribbles > 1 ? @"%ld items" : @"%ld item",
                       (long)numberOfScribbles]];
    
}

#pragma mark ---- UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    CGFloat numberOfPlaceholders = [ScribbleThumbnailCell numberOfPlaceHolders];
    NSInteger numberOfScribbles = [scribbleManager_ numberOfScribbles];
    NSInteger numberOfRows = ceil(numberOfScribbles / numberOfPlaceholders);
    return numberOfRows;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    ScribbleThumbnailCell *cell = (ScribbleThumbnailCell *)[tableView
                                                            dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[ScribbleThumbnailCell alloc] initWithStyle:UITableViewCellStyleDefault
                                             reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    // Configure the cell...
    
    // populate  thumbnails in each cell
    
    // get max number of thumbnail a thumbnail
    // cell can support
    NSInteger numberOfSupportedThumbnails = [ScribbleThumbnailCell numberOfPlaceHolders];
    
    // we can only add max numberOfSupportedThumbnails
    // at a time in each cell
    // e.g. numberOfSupportedThumbnails = 3
    // thumbnail index in the scribble manager is (row index *3) +0, +1, +2
    NSUInteger rowIndex = [indexPath row];
    NSInteger thumbnailIndex = rowIndex *numberOfSupportedThumbnails;
    NSInteger numberOfScribbles = [scribbleManager_ numberOfScribbles];
    for (NSInteger i = 0; i < numberOfSupportedThumbnails &&
         (thumbnailIndex + i) < numberOfScribbles; ++i)
    {
        UIView *scribbleThumbnail = [scribbleManager_ scribbleThumbnailViewAtIndex:
                                     thumbnailIndex + i];
        [cell addThumbnailView:scribbleThumbnail atIndex:i];
    }
    
    return cell;
}

#pragma mark ---- UITableView  Delegeta
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSLog(@"did select row at index");
}

#pragma mark ----  点击 Done 按钮
- (IBAction)onDoneBtnClick:(id)sender
{
    //   
    [[CoordinatingController sharedInstance] requestViewChangeByObject:sender];
}



@end
